<template>
  <div class="app-container">
    <el-table v-loading="loading" :data="configList" max-height="450">
      <el-table-column label="优先级" prop="priority"  align="center"/>
      <el-table-column
        label="规则名称"
        prop="configName"
        min-width="120"
        :show-overflow-tooltip="true"
        align="center"
      />
      <el-table-column label="预设值（大于以下数值生效）" prop="configValue" align="center">
        <template slot-scope="scope">
          <span style="margin-right: 6px">{{ scope.row.configValue }}</span>
          <el-button
            size="mini"
            type="text"
            @click="handleEditConfigValue(scope.row)"
          >
            <i class="el-icon-edit"></i>
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="启用状态" prop="enable" align="center">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.enable" disabled></el-switch>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        width="150px"
        fixed="right"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-top"
            :disabled="scope.row.priority === 1"
            @click="handleUp(scope)"
          >
            上移
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-bottom"
            :disabled="scope.row.priority === 4"
            @click="handleDown(scope)"
            >下移</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="修改预设值" :visible.sync="openConfigValue" width="300px">
      <el-form
        ref="configValueform"
        :model="form"
        :rules="rules"
        label-width="64px"
      >
        <el-form-item label="预设值" prop="configValue">
          <el-input
            placeholder="请输入预设值"
            v-model="form.configValue"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" size="mini" @click="submitConfigValueForm"
          >确 定</el-button
        >
        <el-button size="mini" @click="hanldeConfigValuecancel"
          >取 消</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listMarkups,
  updateMarkups,
} from "@/api/service/config-center/scheduing.js";

export default {
  name: "Config",
  data() {
    return {
      openConfigValue: false,
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 自动排产配置表格数据
      configList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      form: {
        configValue: null,
      },
      rules: {
        configValue: [
          {
            required: true,
            validator: (rule, value, callback) => {
              console.log(value);
              if (value !== "" && value !== null && value !== undefined) {
                if (/^\d+$/.test(value) == false) {
                  callback(new Error("只允许输入数字"));
                } else {
                  callback();
                }
              } else {
                callback();
              }
            },
            trigger: "change",
          },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleUp(scope) {
      const upRow = this.configList[scope.$index - 1];
      updateMarkups([
        { ...scope.row, priority: upRow.priority },
        { ...upRow, priority: scope.row.priority },
      ])
        .then((res) => {
          this.$message.success("上移成功");
          this.getList();
        })
        .catch((e) => {
          this.$message.error(e.message);
        })
        .finally(() => {});
    },
    handleDown(scope) {
      const dowRow = this.configList[scope.$index + 1];
      updateMarkups([
        { ...scope.row, priority: dowRow.priority },
        { ...dowRow, priority: scope.row.priority },
      ])
        .then((res) => {
          this.$message.success("下移成功");
          this.getList();
        })
        .catch((e) => {
          this.$message.error(e.message);
        })
        .finally(() => {});
    },
    hanldeConfigValuecancel() {
      this.openConfigValue = false;
    },
    handleEditConfigValue(record) {
      this.openConfigValue = true;
      this.form = {
        ...this.form,
        ...record,
        configValue: Number(record?.configValue),
      };
    },
    submitConfigValueForm() {
      this.$refs.configValueform.validate((valid) => {
        if (valid) {
          this.fetchUpdateMarkups();
        } else {
          return false;
        }
      });
    },
    fetchUpdateMarkups() {
      updateMarkups([{ ...this.form }])
        .then((res) => {
          this.$message.success("修改成功");
          this.openConfigValue = false;
          this.getList();
        })
        .catch((e) => {
          this.$message.error(e.message);
        })
        .finally(() => {
          this.openConfigValue = false;
        });
    },
    /** 查询自动排产配置列表 */
    getList() {
      this.loading = true;
      listMarkups().then((response) => {
        this.configList = response.data;
        this.loading = false;
      });
    },
  },
};
</script>
